package net.huashitong.buildedProject.dao;

import com.sirdc.modules.core.dao.StringDao;
import net.huashitong.buildedProject.dataBean.ProjectProgressDataBean;
import net.huashitong.buildedProject.entity.ProjectProgress;
import net.huashitong.buildedProject.filter.ProjectProgressFilter;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ProjectProgressDao extends StringDao<ProjectProgress> {

    /**
     * 进度查询（关联项目表）
     * @param filter
     * @return
     */
    public List<ProjectProgressDataBean> queryProjectProgressDataBean(ProjectProgressFilter filter) {

        StringBuffer sql = new StringBuffer();
        sql.append("" +
                "select \n" +
                "pp.sys_id as sysId,\n" +
                "pp.create_date as createDate,\n" +
                "pp.create_time as createTime,\n" +
                "pp.create_user as createUser,\n" +
                "pp.upd_date as updDate,\n" +
                "pp.upd_time as updTime,\n" +
                "pp.upd_user as updUser,\n" +
                "p.address as address,\n" +
                "p.contact_name as contactName,\n" +
                "p.contact_phone as contactPhone,\n" +
                "p.describes as describes,\n" +
                "p.industry_type as industryType,\n" +
                "p.money,\n" +
                "p.name,\n" +
                "p.nature_type as natureType,\n" +
                "p.project_type as projectType,\n" +
                "p.res_dept as resDept,\n" +
                "p.res_user as resUser,\n" +
                "p.sched_code as schedCode,\n" +
                "p.status,\n" +
                "p.user_id as userId,\n" +
                "p.owner_unit_person as ownerUnitPerson,\n" +
                "p.owner_unit_person_phone as ownerUnitPersonPhone,\n" +
                "p.project_classification as projectClassification,\n" +
                "p.fact_finish_date as factFinishDate,\n" +
                "p.plan_begin_date as planBeginDate,\n" +
                "p.res_phone as resPhone,\n" +
                "p.position,\n" +
                "p.nature_type_detail as natureTypeDetail,\n" +
                "p.occupy_dept as occupyDept,\n" +
                "p.occupy_operate_user as occupyOperateUser,\n" +
                "p.occupy_operate_user_phone as occupyOperateUserPhone,\n" +
                "p.occupy_res_user as occupyResUser,\n" +
                "p.occupy_res_user_phone as occupyResUserPhone,\n" +
                "p.group_code as groupCode,\n" +
                "p.group_name as groupName,\n" +
                "p.start_time as startTime,\n" +
                "pp.date_time as dateTime,\n" +
                "pp.investment as investment,\n" +
                "pp.month,\n" +
                "pp.progress as progress,\n" +
                "pp.project_id as projectId,\n" +
                "pp.report_user as reportUser,\n" +
                "pp.report_user_id as reportUserId,\n" +
                "pp.year\n" +
                " from t_bnp_new_project p, t_bnp_project_progress pp\n" +
                "where p.sys_id = pp.project_id ");

        //关联表做分页处理
        if (filter.getPaging() != null) {
            if (filter.getPaging().getSortCol() == null || "".equals(filter.getPaging().getSortCol())) {
                sql.append(" ORDER BY  str_to_date( CONCAT(t1.upd_date,' ',t1.upd_time),'%Y-%m-%d %H:%i:%s') desc  ");
            }
        }

        String hql = sql.toString();
        hql = new StringBuffer(" select count(1) ").append(" from ( ")
                .append(hql).append(" ) temp ").toString();
        Query getCount = getSession().createSQLQuery(hql);
        if (filter.getPaging() != null) {
            int count = ((Number) getCount.uniqueResult()).intValue();
            int resultSize = count;
            int size = filter.getPaging().getPageSize();
            int page = filter.getPaging().getCurrentPage();
            int MaxPage = resultSize / size;
            if (!(resultSize % size == 0)) {
                MaxPage = (resultSize / size) + 1;
            }
            if (page > MaxPage) {
                filter.getPaging().setCurrentPage(MaxPage);
            }
            filter.getPaging().setRecords(resultSize);
        }

        SQLQuery query = getSession().createSQLQuery(sql.toString());
        query.setResultTransformer(Transformers.aliasToBean(ProjectProgressDataBean.class));
        if (filter.getPaging() != null) {
            query.setFirstResult(filter.getPaging().getStartRow());
            query.setMaxResults(filter.getPaging().getPageSize());
        }
        List<ProjectProgressDataBean> result = query.list();
        return result;
    }

}
